package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.Result;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.dao.TemporaryOrdermxDao;
import io.renren.dto.*;
import io.renren.entity.TemporaryOrdermxEntity;
import io.renren.entity.UserEntity;
import io.renren.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 临时采购明细表 
 *
 * @author 翟文闯 619556390@QQ.com
 * @since 1.0.0 2019-12-31
 */
@Service
public class TemporaryOrdermxServiceImpl extends CrudServiceImpl<TemporaryOrdermxDao, TemporaryOrdermxEntity, TemporaryOrdermxDTO> implements TemporaryOrdermxService {
    @Autowired
    private TemporaryOrdermxService temporaryOrdermxService;
    @Autowired
    private TemporaryOrderService temporaryOrderService;
    @Autowired
    private TemporaryOrdermxDao temporaryOrdermxDao;
    @Autowired
    private SmallCanteenPurchasemxService smallCanteenPurchasemxService;
    @Autowired
    private StockOrdermxService stockOrdermxService;
    @Autowired
    private AuxiliaryMaterialsmxService auxiliaryMaterialsmxService;
    @Autowired
    private CollarmxService collarmxService;
    @Autowired
    private FoodmxService foodmxService;
    @Autowired
    private FoodService foodService;
    @Override
    public QueryWrapper<TemporaryOrdermxEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<TemporaryOrdermxEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public Map getTemOrdermx(TemporaryOrdermxDTO dto, UserEntity entity) {
        Map map = new HashMap<>();
        TemporaryOrderDTO temOrder = temporaryOrderService.getTemOrder(dto.getSign(),entity.getId(), entity.getDeptId());
        if(null ==temOrder){
            Result result = new Result();
            result.setCode(5001);
            result.setMsg("草稿表中没有数据");
            map.put("result",result);
          return map ;
        }
        map.put("temOrder",temOrder);
        List<Map> list=new ArrayList<>();
        Map map1=new HashMap();
        List<TemporaryOrdermxDTO> temOrderMxList = temporaryOrdermxDao.getTemsupplier(temOrder.getTemporaryId());
        for (TemporaryOrdermxDTO ordermxDTO:temOrderMxList) {
            map1 = new HashMap();
            if(3==temOrder.getSign()){
                map1.put("chedName",entity.getRealName());
                map1.put("chedId",entity.getId());
                List<TemporaryOrdermxDTO> temOrderCollMx = temporaryOrdermxDao.getTemOrderCollMx(ordermxDTO.getTemporaryId());
                for (TemporaryOrdermxDTO ordermxDTO1:temOrderCollMx) {
                    FoodmxDTO totaoNum = foodmxService.getTotaoNum(ordermxDTO1.getFoodId(), entity.getDeptId());
                    ordermxDTO1.setTotalNum(totaoNum.getTotaoNumber());
                }
                map1.put("foods",temOrderCollMx);
            }else {
                map1.put("supplierName",ordermxDTO.getSupplierName());
                map1.put("supplierId",ordermxDTO.getSupplierId());
                map1.put("foods",temporaryOrdermxDao.getTemOrderMx(ordermxDTO.getTemporaryId(), ordermxDTO.getSupplierId()));
            }
            list.add(map1);

        }
        map.put("list",list);
        System.out.println(map);
        return map;
    }

    /**
     * 根据id删除所有的数据
     * @param temporaryId
     */
    @Override
    public void deleteOrder(Long temporaryId) {
       temporaryOrdermxDao.deleteOrder(temporaryId);
    }

    @Override
    public TemporaryOrdermxDTO getTemOrd(Long temporaryId, Long foodId) {
        TemporaryOrdermxDTO temOrd = temporaryOrdermxDao.getTemOrd(temporaryId, foodId);
        return temOrd;
    }

    @Transactional
    public void saveFood(TemporaryOrdermxDTO dto, UserEntity entity) {
        System.out.println(dto);
        if(null ==dto.getOrderId()){
            TemporaryOrderDTO temOrder = temporaryOrderService.getTemOrder(dto.getSign(), entity.getId(),entity.getDeptId());
            if(null ==temOrder){
                TemporaryOrderDTO temporaryOrderDTO = new TemporaryOrderDTO();
                temporaryOrderDTO.setSign(dto.getSign());
                temporaryOrderDTO.setCanteenId(entity.getDeptId());
                temporaryOrderDTO.setChedId(entity.getId());
                temporaryOrderDTO.setCreateTime(new Date());
                temporaryOrderService.save(temporaryOrderDTO);
            }
            temOrder = temporaryOrderService.getTemOrder(dto.getSign(), entity.getId(),entity.getDeptId());
            dto.setTemporaryId(temOrder.getTemporaryId());
            TemporaryOrdermxDTO temOrd = temporaryOrdermxService.getTemOrd(temOrder.getTemporaryId(), dto.getFoodId());
            if(null == temOrd){
                temporaryOrdermxService.save(dto);
            }{
                temporaryOrdermxService.update(dto);
            }
        } else{
            if(0 == dto.getSign()){
                StockOrdermxDTO stockOrdermxDTO = new StockOrdermxDTO();
                stockOrdermxDTO.setStockOrderId(dto.getOrderId());
                stockOrdermxDTO.setSupplierId(dto.getSupplierId());
                stockOrdermxDTO.setFoodId(dto.getFoodId());
                stockOrdermxDTO.setPurchaseNum(dto.getPurchaseNum());
                stockOrdermxService.save(stockOrdermxDTO);
            }else if(1 == dto.getSign()){
                AuxiliaryMaterialsmxDTO materialsmxDTO = new AuxiliaryMaterialsmxDTO();
                materialsmxDTO.setAuxiliaryMaterialsId(dto.getOrderId());
                materialsmxDTO.setSupplierId(dto.getSupplierId());
                materialsmxDTO.setFoodId(dto.getFoodId());
                materialsmxDTO.setPurchaseNum(dto.getPurchaseNum());
                auxiliaryMaterialsmxService.save(materialsmxDTO);
            }else if(2 == dto.getSign()){
                SmallCanteenPurchasemxDTO smallDTO = new SmallCanteenPurchasemxDTO();
                smallDTO.setSmallCanteenPurchaseId(dto.getOrderId());
                smallDTO.setSupplierId(dto.getSupplierId());
                smallDTO.setFoodId(dto.getFoodId());
                smallDTO.setPurchaseNum(dto.getPurchaseNum());
                smallCanteenPurchasemxService.save(smallDTO);
            }else {
                CollarmxDTO order = collarmxService.getOrder(dto.getOrderId(), dto.getFoodId());
                if(null ==order){
                    CollarmxDTO collarmxDTO = new CollarmxDTO();
                    collarmxDTO.setCollarId(dto.getOrderId());
                    //collarmxDTO.setSupplierId(dto.getSupplierId());
                    collarmxDTO.setFoodId(dto.getFoodId());
                    FoodDTO foodDTO = foodService.get(dto.getFoodId());
                    collarmxDTO.setPurchaseNum(dto.getPurchaseNum());
                    collarmxDTO.setCollarNum(dto.getPurchaseNum());
                    collarmxDTO.setFoodName(foodDTO.getFoodName());
                    collarmxService.save(collarmxDTO);
                }else {
                    order.setCollarNum(dto.getPurchaseNum()+order.getCollarNum());
                    collarmxService.update(order);
                }

            }
        }
    }

    @Transactional
    public void updateFood(TemporaryOrdermxDTO dto, UserEntity entity) {
        if(null ==dto.getOrderId()){
            TemporaryOrderDTO temOrder = temporaryOrderService.getTemOrder(dto.getSign(), entity.getId(),entity.getDeptId());
            TemporaryOrdermxDTO temOrd = temporaryOrdermxService.getTemOrd(temOrder.getTemporaryId(), dto.getFoodId());
            //效验数据
            ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
            temOrd.setPurchaseNum(dto.getPurchaseNum());
            temporaryOrdermxService.update(temOrd);
        }else{
            if(0 == dto.getSign()){
                stockOrdermxService.updateOrder2(dto.getOrderId(),dto.getFoodId(),dto.getPurchaseNum());
            }else if(1 == dto.getSign()){
                auxiliaryMaterialsmxService.updateOrder2(dto.getOrderId(),dto.getFoodId(),dto.getPurchaseNum());
            }else if(2 == dto.getSign()){
                smallCanteenPurchasemxService.updateOrder2(dto.getOrderId(),dto.getFoodId(),dto.getPurchaseNum());
            }else {
                collarmxService.updateOrder2(dto.getOrderId(),dto.getFoodId(),dto.getPurchaseNum());
            }
        }
    }

    @Transactional
    public void deleteFood(TemporaryOrdermxDTO dto, UserEntity entity) {
        if(null ==dto.getOrderId()){
            TemporaryOrderDTO temOrder = temporaryOrderService.getTemOrder(dto.getSign(), entity.getId(),entity.getDeptId());
            TemporaryOrdermxDTO temOrd = temporaryOrdermxService.getTemOrd(temOrder.getTemporaryId(), dto.getFoodId());
            //效验数据
            ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
            dto.setTemporarymxId(temOrd.getTemporarymxId());
            temporaryOrdermxService.deleteById(dto);
        }else {
            if(0 == dto.getSign()){
                stockOrdermxService.delectOrder(dto.getOrderId(),dto.getFoodId());
            }else if(1 == dto.getSign()){
                auxiliaryMaterialsmxService.delectOrder(dto.getOrderId(),dto.getFoodId());
            }else if(2 == dto.getSign()){
                smallCanteenPurchasemxService.delectOrder(dto.getOrderId(),dto.getFoodId());
            }else {
                collarmxService.delectOrder(dto.getOrderId(),dto.getFoodId());
            }
        }
    }
}